let app = getApp();
Page({
  data: {
    bankCardName: "",
    bankCardNumber: "",
    bankName: "",
    cardName: "",
    bankCardNumber: "",
    cashAmount: 0,
    allMoney: 0,
    moneyFlag:""
  },
  depositFocus(event) {
    event.detail.value == 0 ? this.setData({
      cashAmount: ""
    }) : "";
  },
  depositBlur(event){
    let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if(reg.test(event.detail.value)){
      this.setData({ cashAmount: event.detail.value });
    }else{
      this.setData({cashAmount: ''});
      return app.toast('请输入正确的金额')
    }
  },
  allDeposit(event) {
    this.setData({
      cashAmount: this.data.allMoney
    })
  },
  formSubmit(e) {    
    let {
      bankCardName,
      bankCardNumber,
      bankName,
      cardName,
      cashAmount
    } = e.detail.value;
    this.setData({
      warn: "",
      bankCardName,
      bankCardNumber,
      bankName,
      cardName,
      cashAmount: cashAmount
    });
    this.submitDeposit();
  },
  submitDeposit() {
    let depositData = {
      "bankCardName": this.data.bankCardName,
      "bankCardNumber": this.data.bankCardNumber,
      "bankName": this.data.bankName,
      "cardName": this.data.cardName,
      "cashAmount": Number(this.data.cashAmount) * 100
    };
    if (!this.data.bankCardName) {
      return app.toast("请在输入银行名称");
    } else if (!this.data.bankCardNumber) {
      return app.toast("请在输入银行卡号");
    } else if (!this.data.bankName) {
      return app.toast("请在输入银行卡支行");
    } else if (!this.data.cardName) {
      return app.toast("请在输入持卡人姓名");
    } else if (this.data.cashAmount <=0) {
      return app.toast("请在输入提现金额");
    } else {    
      wx.showModal({      
        title: '提示',
        content:'是否提现',
        success: (res) => {      
          if (res.confirm) {
            app.post("accountConfirm", depositData).then((res) => {
              wx.hideNavigationBarLoading();
              app.toast(res.message);
              setTimeout(()=>{
                wx.switchTab({ url: "/pages/mine/mine" });
              },1000)
            }).catch((e) => {
              wx.hideNavigationBarLoading();
              app.toast("网络或服务器错误");
            });
          }
          else if (res.cancel) {   
            app.toast('取消提现'); 
          }            
        }        
      })
    }
  },
  onLoad(options) {
    this.setData({allMoney: options.balance})
    app.checkLogin();
  }
})